package io.renren.config.feign;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import feign.Logger;
import feign.codec.Decoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import java.text.SimpleDateFormat;

@Configuration
public class FeignConfig {
  @Bean
  Logger.Level feignLoggerLevel() {
    //这里记录所有，根据实际情况选择合适的日志level
    return Logger.Level.FULL;
  }

  @Bean
  @Primary
  @Scope(BeanDefinition.SCOPE_PROTOTYPE)
  FeignInterceptor feignInterceptor() {
    return new FeignInterceptor();
  }

  @Bean
  public Decoder decoder(){
    ObjectMapper objectMapper = new ObjectMapper();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    objectMapper.setDateFormat(simpleDateFormat);
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    MappingJackson2HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter(objectMapper);
    ObjectFactory<HttpMessageConverters> objectFactory = ()->(new HttpMessageConverters(jacksonConverter));
    return new SpringDecoder(objectFactory);
  }
}
